/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */ 
package controller;

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;


/**
 *
 * @author Phillipp
 */
public class WriteXMLFileCourse {
 
	public static void main(String coursename, int credits, String courseDescription, String coursetype) throws SAXException, IOException, ParserConfigurationException, TransformerConfigurationException, TransformerException {
 
	DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        
        try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("./web/courseXML.xml");
        Element root = document.getDocumentElement();

        
        Element rootElement = document.getDocumentElement();

        
            Element course = document.createElement("Course");
            rootElement.appendChild(course);

            Element courseType = document.createElement("CourseType");
            courseType.appendChild(document.createTextNode(coursetype));
            course.appendChild(courseType);

            Element module = document.createElement("Module");
            module.appendChild(document.createTextNode("nicht implementiert"));
            course.appendChild(module);

            Element courseName = document.createElement("Coursename");
            courseName.appendChild(document.createTextNode(coursename));
            course.appendChild(courseName);


            Element cDescription = document.createElement("Coursedescription");
            cDescription.appendChild(document.createTextNode(courseDescription));
            course.appendChild(cDescription);
            
            try{
            Element creditPoints = document.createElement("Creditpoints");
            Integer myInt = new Integer(credits);
            String cp = myInt.toString();
            creditPoints.appendChild(document.createTextNode(cp));
            course.appendChild(creditPoints); 
            }
            catch(Exception e){
                System.out.println("Error while saving creditpoints");
            }

            root.appendChild(course);

        DOMSource source = new DOMSource(document);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        StreamResult result = new StreamResult("./web/courseXML.xml");
        transformer.transform(source, result);
        System.out.println("Saved!");
        
         } catch(ParserConfigurationException | SAXException | IOException ex) {
        }
    }
}